﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using Fulcrum;

namespace FulcrumConsole
{
    class Program
    {
        public Program()
        {
            // nothing
        }

        static void Main(string[] args)
        {
            try
            {
                var program = new Program();

                program.Run(args);
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occured: {0}", e.Message);
                Console.WriteLine();
                Console.WriteLine(e);
            }
        }

        private void Run(string[] args)
        {
            var settings = new FulcrumSettings();
            var unitManager = new FulcrumGenerationUnitManager();

            unitManager.LoadGenerationUnits();

            var processor = new CommandLineProcessor("FulcrumConsole", settings, unitManager);

            if (args.Length < 1)
            {
                processor.ShowUsage();
                return;
            }

            try
            {
                processor.ParseCommandLine(args);
            }
            catch
            {
                processor.ShowUsage();
                Console.WriteLine();
                throw;
            }

            var engine = new FulcrumEngine(settings, unitManager.GetGenerationUnit(settings.Module));

            engine.Run();

            Console.WriteLine("Code generated.");
        }
    }
}
